﻿using System.Windows.Controls;
using DevKnightlie.WPF.AutoForm.FieldDefinitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DevKnightlie.WPF.AutoForm.Tests
{
    [TestClass]
    public class SubFormFieldDefinitionTests
    {
        [TestMethod]
        public void SubFormInStackedLayoutTest()
        {
            StackedLayoutStrategy strategy = new StackedLayoutStrategy();
            StackedLayoutStrategy strategy2 = new StackedLayoutStrategy();
            FieldDefinitionList fieldDefs = new FieldDefinitionList()
            {
                new TextFieldDefinition() {Title="Name", PropertyName = "Name"},
                new SubFormFieldDefinition()
                {
                    Title="Subform",
                    LayoutStrategy = strategy2,
                    FieldDefinitionList = new FieldDefinitionList()
                    {
                        new TextFieldDefinition() {Title="Address 1", PropertyName = "Address1"},
                        new TextFieldDefinition() {Title="Address 2",PropertyName = "Address2"}
                    }
                }
            };
            AutoFormPanel panel = new AutoFormPanel()
            {
                FieldDefinitions = fieldDefs,
                LayoutStrategy = strategy
            };
            panel.RefreshForm();
            /* Structure (layout is stacked, so there are no DockPanels):
             * Panel
             *   Grid
             *     Label
             *     TextBox
             *     GroupBox
             *       Label
             *       TextBox
             *       Label
             *       TextBox
             */
            Grid container = panel.Content as Grid;
            Assert.IsNotNull(container);
            Assert.AreEqual(3, container.Children.Count, "Unexpected number of children in the grid");
            Assert.IsTrue(container.Children[0] is Label);
            Assert.IsTrue(container.Children[1] is TextBox);
            Assert.IsTrue(container.Children[2] is GroupBox);
            GroupBox subFormBox = container.Children[2] as GroupBox;
            Assert.IsTrue(subFormBox.Content is AutoFormPanel);
            AutoFormPanel panel2 = subFormBox.Content as AutoFormPanel;
            panel2.RefreshForm(); // Controls won't have been created yet, so do it manually
            Grid container2 = panel2.Content as Grid;
            Assert.IsNotNull(container2);
            Assert.AreEqual(4, container2.Children.Count, "Unexpected number of children in the subform grid");
            Assert.IsTrue(container2.Children[0] is Label);
            Assert.IsTrue(container2.Children[1] is TextBox);
            Assert.IsTrue(container2.Children[2] is Label);
            Assert.IsTrue(container2.Children[3] is TextBox);
        }
    }
}
